Central Article

Architecting Applications in Macromedia Central

Michael Williams

Michael Williams

Macromedia

Over the short history of web software development we have seen many of the same patterns and solutions emerge that were and still are commonplace in developing desktop applications. We started with simple web applications that displayed dynamic data based upon user input. Over time software developers began to join the fold of web application developers and the demands for more stateful and functional clients increased as the population of the Internet grew. The problem of refactoring and reuse became more and more troublesome as the size and complexity of web applications grew as well. Developers began to adopt tried and true methodologies such as object-oriented design and component-based software.

In many ways the Flash development community has followed the same pattern, moving from a simple scripting language in Flash 4 to a fully object-oriented environment with a rich set of user interface and data abstraction components in Macromedia Flash MX 2004. Macromedia Central introduces a potentially new level of complexity to building applications using Macromedia Flash. This article discusses how Central hopes to solve those problems head-on.

Learning Central Essentials

The first step in building any application is understanding the essentials required to get up and running. Creating Central applications requires a strong knowledge of ActionScript, Macromedia Flash, and the environment in which your application will be deployed. Central is the natural evolution of Flash as a client for Internet applications and to realize fully the vision that properly architected applications are now more important than ever.

Service-Oriented Architecture

The heart of Macromedia Central is an asynchronous, message-driven, dynamic runtime-modifiable framework powered by ActionScript. Architecting Central applications is about leveraging existing web repositories for data, as well as driving those web applications that exist today towards a service-oriented architecture. In a service-oriented architecture the user interface becomes abstracted from the data model in such a way that design can be the focus when design matters, and data can be the focus when data matters.

The majority of existing web applications use a model that makes a lot of sense when working with a single monolithic client: the web browser. For that model to work in a maintainable fashion, both client and server must work very closely together to give the illusion of state in a client that is virtually stateless.

Central introduces the idea of a truly stateful client that can host applications that function regardless of server state. With this type of flexibility on the client, developers are left with the task of presenting data in an elegant and usable fashion. This is where a service-based architecture comes into play.

For instance, let's say you want to track your current tasks but also allow your manager to view the status of those items at any given time. If you built a traditional web application for this task we would have to take into account both the data model on the server side, the user interface for data entry, and the user interface for data retrieval. By contrast, with a service-based architecture and Central as the client of that service, you only need to build the data service and the client application that reads and writes to that service.

Flash Player

Macromedia Central is based upon and tightly integrated with Flash Player. This allows existing Flash content, as well as Central content, to run side by side with little or no modification. At the same time this gives Central the flexibility to integrate with the native operating system in ways that would be difficult when running within the constraints of the web browser.

For example, Central 1.0 allows developers to apply native tooltips to any movie clip within their project using a simple getter/setter property. In the past, when Flash developers wanted to apply tooltip functionality to their applications, they were forced to create tooltip-like functionality within the constraints of Flash Player and its capabilities. But now with this type of close integration with native system services, Macromedia enables developers to leverage their existing knowledge of ActionScript development to build and deploy applications using the flexibility and familiarity of native system services. Over time, Central will expand this integration and allow a whole new class of applications to be developed using existing skill sets and tools.

Central Components

The components in Macromedia Central are based on the first generation of user interface components that shipped with Flash MX. They have been extended to support the Central environment as well as address key issues that exist when building large-scale applications. One of the major changes in how Central uses these components, compared to using the web player, is their use as a global runtime shared library. Once the initial installation of Central takes place, all developers who use Central components will automatically have the components available to them at runtime, locally cached on the end-user's client. This solves the issue of many applications using a single set of user interface elements without forcing each application running within the environment to download those same elements. As a developer you are still able to use whichever components you wish, but if you want to leverage the existing base of interface components for a common look, feel, and behavior, Central components are the way to go.

Primary Elements

Each Central application is made up of three primary parts: the product.xml file that serves as a manifest of your application and its elements, the SWFs that make up your application, and the icon that represents your application in Central:

  • product.xml: The product.xml file is a standard XML file that informs Central of how you wish to install your application and what files should be cached locally for it to execute properly.
  • Compiled SWFs: Central currently supports SWF content that targets any version up to and including Flash Player 6.0r79 (the final release before Flash Player 7).
  • Application: This is the most visible element of your Central application. The application SWF is loaded into the primary application shell whenever the user requests your application using the tool bar, menu, or My Applications.
  • Agents: These run in a separate instance of the Central Flash Player and allow your application to provide services and data management both while your application is running and while it is not.
  • Pods: These are loaded into the console and present a small persistent user interface into your larger application that allows users to monitor key data points without having to take up a large amount of desktop real estate.

Architecting Well-Behaved Applications

Let's dig into what it takes to build a well-behaved application in Macromedia Central and how you use agents, applications, and pods together to provide a truly unique user experience.

Flexibility

By building applications using Central you afford yourself some flexibility that's similar to web applications which allow you to deploy once to a single client and present your interface in a consistent way on a variety of platforms. The initial release of Central supports Windows 98 through Windows XP, as well as Mac OS 10.1 and above. By building applications that leverage Macromedia's strengths in cross-platform deployment, you free yourself from one of the major limiting factors in building applications that are more tightly integrated with the native operating system.

Reusability

When you build the types of applications that Central makes possible, a key factor that comes into play is code reuse and refactoring. As Flash applications increase in complexity, there needs to be an equal amount of effort put into maintaining those applications. By building reusable code you invest in your existing and future applications that will be deployed with Flash Player. However, real code reuse is something of a myth in programming circles. Here are a few ideas on building reusable code in the Flash world.

If you find yourself duplicating common code across your application, put some effort into building a reusable function to which you can point all your various event handlers. By encapsulating your code in a function (or class in ActionScript 2.0) you make your code more readable and approachable to others who may work with your code base in the future. This becomes even more important when you build applications in Central.

For example, suppose you build an agent that stores local to-do lists on the client. It's natural for you as a developer to give that agent clear interfaces for getting and setting to-do items. The agent itself is no longer tied to your application, as it would be running inside a browser where everything is unified on a single page. Using the example of an agent SWF acting as a local service to any Central application, here is a bit of code to illustrate the point.

Sample code for myAgent.swf:

// build interface for local to-do service
var IToDoService = {};
IToDoService.name = "com.macromedia.samples.IToDoService";
ITestService.interfaces = {};
ITestService.interfaces["Server"] = [ "setToDo", "getToDo" ];

// define callback functions 
function setToDo( item, description )
{ 
   // set to-do and store in local shared object
   this.fToDo = foo;
}
 function getToDo() { 
   // get to-do list from local shared object
   return this.fToDo; 
} 

// called on activation of your local service agent
function onActivate( agentManager, agentId, initialData )
{
   this.fAgentManager = agentManager; 
   // create your local service connection using LCService 
   // registering as a server
   this.fService = Central.LCService.createServer( IToDoService, this ); } 
}

Sample code for myCentralApplication.swf:

function onActivate( shell, appId, shellId, bastTabIndex, initialData )
{
      this.fShell = shell;
      this.fAppId = appId;
      this.fShellId = shellId;

      // Generate unique client id for service connection
      this.fClientId = String(this.fAppId+"_"+this.fShellId);

      // create your local service connection using LCService
      // registering as a client
      this.fService = Central.LCService.createClient( IToDoService, this.fClientId, this );
}

function displayToDoList()
{
   var fToDoList = this.fService.getToDo();
   // display data as needed
}

function onDeactivate()
{
   // CLEAN up my service connection
   this.fService.close();
}

This example uses LCService to tie the application SWF directly to the agent SWF without having to deal directly with LocalConnection or any form of server proxy. For more information on how to build service-oriented applications using LCService, check out Ethan Malasky's article, "Understanding and Using the LCService Library."

Interapplication Communication

Building applications around services leads directly into a discussion of how best to allow applications to communicate with each other even when they may have been built by different parties running in completely different domains.

Agents allow any SWF running in the Central environment, or even in the browser, to communicate seamlessly across domains and players with an easy-to-use synchronous programming interface. This technique does not currently offer any form of service discovery, however, so applications that wish to utilize service agents need to be aware of the interfaces and the overall API of your service.

Another way to communicate among applications that may not have any shared development is through the use of Blast. The Blast feature in Central provides a way for applications to send user-selected data to other applications installed in Central. Applications can listen for this data and respond to it in useful ways.

Following Design Patterns

An important aspect of good application design is the adherence to known design patterns that help enforce relationships among objects, functions, and the state of an application at any given time. Design patterns attempt to solve common problems that happen over and over; by following the common principles of application design patterns, you not only make the development of your application easier but you leave behind an architecture that can be readily modified and expanded over time.

What Is MVC?

One of the more common design patterns in practice today by web and Flash application developers is MVC, or Model View Controller. MVC has done much to help both new and old developers structure highly flexible and reusable projects in a variety of languages and platforms.

An MVC-based application is made up of three distinct parts. While variations exist in how these parts are described, these basic descriptions remain the same:

  • Model: Common representation of the data being presented and manipulated.
  • View: Independent visual presentation of data. Can be one of multiple separate "views" of the model.
  • Controller: Application logic that manipulates the model. Also sometimes associated with GUI elements that the user interacts with to tell the application to perform the action. Within Central it is simpler to think of the controller as a programmatic interface (using an ActionScript accessible API) and that UI elements are a distinct part of the view.

Figure 1 depicts a typical MVC application running in the Central environment. You may have one or many separate views into a unified model/controller that exists within your agent. By unifying both local and remote data access into your agent, you allowing easy access into the core logic of your application no matter how it's presented to the user within Central.

A typical MVC application running in the Central environment

Figure 1. A typical MVC application running in the Central environment

Advantages of Using MVC

There are several advantages to using the MVC model:

  • Clarity of design: When you build your application using clear interfaces into local and remote services defined within the model, anyone can simply glance at the methods of the model to get a clear understanding of what your application provides.
  • Ease of reuse: By breaking your application into distinct pieces with clear methods and interfaces, you make your life much easier if and when you find yourself in a situation where you need to solve the same problem in a different situation.
  • Scalability: As an application grows in complexity, the controller and model can change, while the view remains the same. Complex data logic is abstracted from the view.
  • Multiple views on a single model/controller: Once you have a known controller and data model, you can build a variety of different user interface views into that model. This permits tremendous flexibility in user interface design while allowing the overall data model to remain transparent to what needs to be done on the client to present that data.

Making MVC Feel Right

Using MVC in the Central environment provides a clear way to build applications. The separation of model from view is made approachable by the clear physical separation of the application from the agent. By placing your model and controller into your agent, you must explicitly make requests from your agents to receive information on the current state of the data that drives your application.

Going back to the to-do list application example, since you placed access to the to-do list data in the agent, the application—which represents the view in this case—must make requests to the agent data model to get or set the list of current tasks. Because this data model is available to any application that's aware of the to-do data model, you can build a number of different views on that model/controller without making any changes to the agent that contains it. For instance, if you wanted also to have a pod that shows the current list of to-do items, you could simply move the view code into a pod SWF and use the same interfaces into the model that you would if you were in the application view. This allows for multiple applications running in a variety of contexts to affect a repository of data and see that data updated in real time.

Macromedia Flash continues to evolve over time with the guidance of a community of both designers and developers who are shaping the web as we know it. With Central, Macromedia facilitates a whole new class of applications that, until now, were highly limited by the browser sandbox in which they resided. Central will continue to grow as the best place to deploy truly rich applications. With the tools and framework that Central provides, the power to build fundamentally better applications is finally starting to see the light of day. This is just the beginning of great things to come.


About the author

Michael Williams is a software engineer at Macromedia. He is responsible evangelizing and implementing technologies related to Macromedia Flash. Prior to Macromedia, Michael spent three years doing research and development at the Walt Disney Company. He was also a member of the Macromedia Flash Advisory Board. At Disney/Go.com, Williams developed and implemented web content and applications for various corporate sites, including Disney.com, Disneyblast.com, Movies.com, ESPN.com, and ABC.com. Michael helped Disney create core systems and standardize on a single platform after the acquisition of Infoseek.